package com.fw.ui.view.settings.usercontrol;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.core.SpringViewProvider;
import com.fw.ui.core.ViewDescription;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.AbstractView;
import com.vaadin.navigator.View;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

@org.springframework.stereotype.Component
@Scope(SCOPE_PROTOTYPE)
@ViewDescription(name = "settings-user-control", path = "/settings/user-control")
public class UserControlView extends AbstractView {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PropertyBAO propertyBAO;

    /**
     * COMPONENT
     */
    private Panel settingUserControlPanel = new Panel();

    @Override
    public void updateStrings() {
	this.settingUserControlPanel.setCaption(this.messageBAO.getMessage("view.settings-user-control.title"));
    }

    @Override
    public void init() {
	this.setSizeFull();

	this.settingUserControlPanel.setSizeFull();
	this.settingUserControlPanel.setId("settingPanel");

	VerticalLayout mainLayout = new VerticalLayout();
	mainLayout.setSizeFull();
	mainLayout.setSizeUndefined();
	mainLayout.setSpacing(true);
	mainLayout.setWidth("100%");
	
	VerticalLayout vLayout = new VerticalLayout();
	vLayout.setSizeUndefined();
	vLayout.setSpacing(true);
	vLayout.setWidth("80%");

	HorizontalLayout horizontalLayout = new HorizontalLayout();
	horizontalLayout.setSpacing(true);
	horizontalLayout.setMargin(true);
	
	ThemeResource iconThemeResource = new ThemeResource("img/icon-person.png");
	Image iconPic = new Image(null, iconThemeResource);
	iconPic.setWidth("120px");
	iconPic.setHeight("120px");
	
	VerticalLayout linkLayout = new VerticalLayout();
	linkLayout.setSpacing(true);
	String mainMenuStrAll = propertyBAO.getPropertyValue("view.settings-user-control.child");
	if (mainMenuStrAll != null) {
	    String[] mainMenuArr = mainMenuStrAll.split(",");
	    for (String mainMenuStr : mainMenuArr) {
		String keyPathStr = "view." + mainMenuStr + ".path";
		String keyTitleStr = "view." + mainMenuStr + ".title";

		final String pathValue = this.propertyBAO.getPropertyValue(keyPathStr);
		String titleValue = this.messageBAO.getMessage(keyTitleStr);

		Button btnMenu = new Button(titleValue);
		btnMenu.setId("btn" + mainMenuStr + "Menu");
		btnMenu.addStyleName("link");
		btnMenu.addClickListener(new ClickListener() {
		    /**
		     * 
		     */
		    private static final long serialVersionUID = 1L;

		    @Override
		    public void buttonClick(ClickEvent event) {
			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
			SpringViewProvider securityViewProvider = (SpringViewProvider) SpringViewProvider
				.createViewProvider((Authentication) authentication);
			View view = securityViewProvider.getView(pathValue);
			if (view != null && !getUI().getNavigator().getState().equals(pathValue)) {
			    getUI().getNavigator().navigateTo(pathValue);
			}
		    }
		});
		linkLayout.addComponent(btnMenu);
		linkLayout.setComponentAlignment(btnMenu, Alignment.MIDDLE_LEFT);
	    }
	}
	
	horizontalLayout.addComponent(iconPic);
	horizontalLayout.addComponent(linkLayout);
	
	horizontalLayout.setComponentAlignment(iconPic, Alignment.MIDDLE_CENTER);
	horizontalLayout.setComponentAlignment(linkLayout, Alignment.MIDDLE_CENTER);

	vLayout.addComponent(horizontalLayout);
	vLayout.setComponentAlignment(horizontalLayout, Alignment.MIDDLE_LEFT);

	mainLayout.addComponent(vLayout);
	mainLayout.setComponentAlignment(vLayout, Alignment.MIDDLE_CENTER);
	
	this.settingUserControlPanel.setContent(mainLayout);

	this.addComponent(this.settingUserControlPanel);
	this.setComponentAlignment(this.settingUserControlPanel, Alignment.TOP_CENTER);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initValidator() {

    }

    @Override
    public void initData() {

    }

}
